package com.craney.util
{
	import flash.display.Sprite;
	
	public class ProgressBar extends Sprite
	{
		/**
		 * 用于显示当前进度的进度条 
		 */		  
		protected var currentProgress:Sprite = new Sprite();
		
		/**
		 * 进度条的总长度
		 */		
		protected var barWidth:uint = 150;
		
		/**
		 * 进度条的总高度 
		 */		
		protected var barHeight:uint = 10;
		
		/**
		 * 当前进度条的进度颜色
		 */		
		protected var currentColor:uint = 0xCCFF00;
		
		/**
		 * 当前进度条颜色透明度
		 */		
		protected var currentColorAlpha:Number = 1.0;
		
		/**
		 * 进度条的背景颜色
		 */		
		protected var totalColor:uint = 0xCCCCFF;
		
		/**
		 * 进度条的背景颜色透明度 
		 */		
		protected var totalColorAlpha:Number = 1.0;
		
		/**
		 * 
		 * @param barWidth 进度条的总长度
		 * @param barHeight 进度条的总高度
		 * @param currentColor 进度条的进度颜色
		 * @param totalColor 进度条的背景颜色
		 * @param currentColorAlpha 进度条进度透明度
		 * @param totalColorAlpha 进度条背景透明度
		 * 
		 */		 		
		public function ProgressBar(barWidth:uint=100, barHeight:uint=15, currentColor:uint=0x00FF00, totalColor:uint=0x0000FF, currentColorAlpha:Number=1.0, totalColorAlpha:Number=1.0)
		{
			super();
			
			//初始化各个属性
			this.barWidth = barWidth;
			this.barHeight = barHeight;
			this.currentColor = currentColor;
			this.totalColor = totalColor;
			this.currentColorAlpha = currentColorAlpha;
			this.totalColorAlpha = totalColorAlpha;
			
			//画出总进度条
			graphics.beginFill(totalColor, totalColorAlpha);
			graphics.drawRect(0, 0, barWidth, barHeight);
			
			//把当前进度条加入显示列表
			addChild(currentProgress);
		}
		
		/**
		 * 
		 * @param percentage 
		 * 参数要求：percentage>=0 && percentage<=1
		 * 小于0按0处理，大于1按1处理
		 * 
		 */		
		public function drawProgress(percentage:Number):void
		{
			//对参数进行处理
			if(percentage < 0)
			{
				percentage = 0;
			}
			else if(percentage > 1)
			{
				percentage = 1;
			}
			//清除当前进度图像
			currentProgress.graphics.clear();
			//指定当前进度颜色
			currentProgress.graphics.beginFill(currentColor, currentColorAlpha);
			//画出当前进度
			currentProgress.graphics.drawRect(0, 0, percentage*barWidth, barHeight);
			
			trace(this, "\tProgress = " + (percentage*100).toFixed(2) + "%");
			
		}




	}
}